package com.c.ctgproject.adapter

import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.HandlerBatteryEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.ui.collect.DictionaryMenuActivity
import com.c.ctgproject.ui.collect.HandlerBatteryViewModel
import com.c.ctgproject.utils.*
import com.lwkandroid.widget.ninegridview.NineGridView

class HandlerBatteryAdapter(private val context: FragmentActivity, private val mList: ArrayList<HandlerBatteryEntity>,private val mViewModel:HandlerBatteryViewModel,private val lisener:PhotoShowListAdapter.DeleteCallBack,private val tListenser:CarPartHandlerAdapterListener) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_handler_battery_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mBatteryFlag.text="电池${position+1}"
            if(mViewModel.isFinish.value==true){
                holder.stockNumStart.visible()
                holder.moduleCountLL.visible()
                holder.moduleCountLine.visible()
                //holder.stockPosiStart.visible()

            }else{
                holder.stockNumStart.gone()
                holder.moduleCountLL.gone()
                holder.moduleCountLine.gone()
                //holder.stockPosiStart.gone()
            }
            if(!data.batteryName.isNullOrEmpty()){
                holder.stockName.text=data.batteryName.handlerNull()
            }else{
                holder.stockName.text="请选择电池名称"
            }

            MyLog.d("数据检测${data.picUrls?.size}")
//            val photos=ArrayList<String>()
//            data.picUrls?.forEach {
//                photos.add(it)
//            }
            val mAdapter=PhotoShowListAdapter(context,data.picUrls,6,lisener,true,position)
            holder.mRecyclerView.layoutManager=StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
            holder.mRecyclerView.adapter=mAdapter
            mAdapter.notifyDataSetChanged()
            holder.mDictionarySelectLL.setOnClickListener {
                DictionaryMenuActivity.jumpToDictionary(context, DictionaryMenuEnum.BATTERY.menuCode, true, arrayListOf(data.batteryName?:""), parentPosition = position)
            }
            if(!data.batteryCode.isNullOrEmpty()){
                holder.batteryCode.setText(data.batteryCode)
            }else{
                holder.batteryCode.text.clear()
            }
//            holder.batteryCode.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.batteryCode=it
//                }
//
//            }
            if(data.count!=null){
                holder.stockNum.setText(data.count.handlerNull())
            }else{
                holder.stockNum.text.clear()
            }
//            holder.stockNum.handlerText {
//                MyLog.d("测试$it $position")
//                if(!it.isNullOrEmpty()){
//                    data.count=it.toInt()
//                }
//            }
            if(data.weight!=null){
                holder.weight.setText(data.weight.handlerNull())
            }else{
                holder.weight.text.clear()
            }
//            holder.weight.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.weight=it.toDouble()
//                }else{
//                    data.weight=0.0
//                }
//
//            }
            if(data.capacityAh!=null){
                holder.capacityAh.setText(data.capacityAh.handlerNull())
            }else{
                holder.capacityAh.text.clear()
            }
//            holder.capacityAh.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.capacityAh=it.toDouble()
//                }
//
//            }
            if(data.voltage!=null){
                holder.voltage.setText(data.voltage.handlerNull())
            }else{
                holder.voltage.text.clear()
            }
//            holder.voltage.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.voltage=it.toDouble()
//                }
//            }
            if(data.capacityKwh!=null){
                holder.capacityKWh.setText(data.capacityKwh.handlerNull())
            }else{
                holder.capacityKWh.text.clear()
            }
            if(data.stockPosi!=null){
                holder.stockPosi.setText(data.stockPosi.handlerNull())
            }else{
                holder.stockPosi.text.clear()
            }
//            holder.capacityKWh.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.capacityKwh=it.toDouble()
//                }
//
//            }
            if(!data.remark.isNullOrEmpty()){
                holder.mRemarkET.setText(data.remark)
            }else{
                holder.mRemarkET.text.clear()
            }
//            holder.mRemarkET.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.remark=it
//                }
//
//            }
            if(data.moduleCount!=null){
                holder.moduleCount.setText(data.moduleCount.handlerNull())
            }else{
                holder.moduleCount.text.clear()
            }
            if(data.bulkInOrderNo!=null){
                holder.bulkInOrderNo.setText(data.bulkInOrderNo.handlerNull())
            }else{
                holder.bulkInOrderNo.text.clear()
            }
//            holder.moduleCount.handlerText {
//                if(!it.isNullOrEmpty()){
//                    data.moduleCount=it.toInt()
//                }
//
//            }
            takeWatch(holder.moduleCount,position,"模组数量",holder)
            takeWatch(holder.stockNum,position,"电池数量",holder)
            takeWatch(holder.batteryCode,position,"电池编码",holder)
            takeWatch(holder.weight,position,"重量",holder)

            takeWatch(holder.capacityAh,position,"容量Ah",holder)
            takeWatch(holder.voltage,position,"电压",holder)
            takeWatch(holder.capacityKWh,position,"容量Kwh",holder)
            takeWatch(holder.mRemarkET,position,"备注",holder)
            takeWatch(holder.stockPosi,position,"库位",holder)
            takeWatch(holder.bulkInOrderNo,position,"入库编码",holder)
            holder.mDeleteIV.setOnClickListener {
                if(mList.size>1){
                    mList.remove(data)
                    tListenser.onBatteryDelete()
                    notifyDataSetChanged()
                }else{
                    context.showToast("至少拆解一个电池")
                }
            }

            //holder.mPhotoNGV.init(context = context, ArrayList<String>(), 6, position)
//            val resultList: MutableList<NineGridBean> = ArrayList()
//            data.picUrls.forEach {
//                val nineGirdData = NineGridBean(it)
//                resultList.add(nineGirdData)
//            }
//            holder.mPhotoNGV.dataList
//            holder.mPhotoNGV.addDataList(resultList)
        }
    }
    fun takeWatch(view:EditText,position:Int,tip:String,holder:MyViewHolder){

        val watcher = object : TextWatcher {

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                if(!view.hasFocus()){
                    MyLog.d("焦点$tip")
                }
                if (view.hasFocus()) {
                    //判断当前EditText是否有焦点在
                    //listener.onNumChange(s.toString(), parentPosition, position)

                    MyLog.d("测试数据$tip")
                    when(tip){
                        "模组数量"->{
                            val moduleCount=view.text.toString()
                            if(!moduleCount.isNullOrEmpty()&&moduleCount.handlerNull().substring(0, 1)!="."){
                                mList[position].moduleCount=moduleCount.toInt()
                            }else{
                                mList[position].moduleCount=null
                            }

                        }
                        "电池数量"->{
                            val stockNum=view.text.toString()
                            if(!stockNum.isNullOrEmpty()&&stockNum.handlerNull().substring(0, 1)!="."){
                                mList[position].count=stockNum.toInt()
                            }else{
                                mList[position].count=null
                            }
                        }
                        "电池编码"->{

                            val batteryCode=view.text.toString()
                            if(!batteryCode.isNullOrEmpty()&&batteryCode.handlerNull().substring(0, 1)!="."){
                                mList[position].batteryCode=batteryCode
                            }else{
                                mList[position].batteryCode=null
                            }
                        }
                        "入库编码"->{

                        val bulkInOrderNo=view.text.toString()
                        if(!bulkInOrderNo.isNullOrEmpty()){
                            mList[position].bulkInOrderNo=bulkInOrderNo
                        }else{
                            mList[position].bulkInOrderNo=null
                        }
                    }
                        "重量"->{

                            val weight=view.text.toString()
                            if(!weight.isNullOrEmpty()&&weight.handlerNull().substring(0, 1)!="."){
                                mList[position].weight=weight.toDouble()
                            }else{
                                mList[position].weight=null
                            }
                        }
                        "容量Ah"->{
                            val capacityAh=view.text.toString()
                            if(!capacityAh.isNullOrEmpty()&&capacityAh.handlerNull().substring(0, 1)!="."){
                                mList[position].capacityAh=capacityAh.toDouble()
                            }else{
                                mList[position].capacityAh=null
                            }

                        }
                        "容量Kwh"->{

                            val capacityKWh=view.text.toString()
                            if(!capacityKWh.isNullOrEmpty()&&capacityKWh.handlerNull().substring(0, 1)!="."){
                                mList[position].capacityKwh=capacityKWh.toDouble()
                            }else{
                                mList[position].capacityKwh=null
                            }
                        }
                        "电压"->{

                            val voltage=view.text.toString()

                            if(!voltage.isNullOrEmpty()&&voltage.handlerNull().substring(0, 1)!="."){
                                mList[position].voltage=voltage.toDouble()
                            }else{
                                mList[position].voltage=null
                            }

                        }
                        "备注"->{
                            val remark=view.text.toString()
                            if(!remark.isNullOrEmpty()){
                                mList[position].remark=remark
                            }else{
                                mList[position].remark=null
                            }
                        }
                        "库位"->{
                            val stockPosi=view.text.toString()

                            if(!stockPosi.isNullOrEmpty()){
                                mList[position].stockPosi=stockPosi
                            }else{
                                mList[position].stockPosi=null
                            }
                        }
                    }
                    //mList[position].carEngine=holder.mCarEnergyET.text.toString()
                }
            }
        }
        view.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                view.addTextChangedListener(watcher)
            } else {
                when(tip){
                    "容量Ah"->{
                        if(mList[position].voltage!=null&&mList[position].capacityAh!=null){
                            val result=(mList[position].capacityAh!!*mList[position].voltage!!)/1000
                            mList[position].capacityKwh=result
                            holder.capacityKWh.setText(result.handlerNull())
                            //Handler().post(Runnable { notifyDataSetChanged() })
                        }
                    }

                    "电压"->{
                        if(mList[position].voltage!=null&&mList[position].capacityAh!=null){
                            val result=(mList[position].capacityAh!!*mList[position].voltage!!)/1000
                            mList[position].capacityKwh=result
                            holder.capacityKWh.setText(result.handlerNull())
                            //Handler().post(Runnable { notifyDataSetChanged() })
                        }
                    }

                }
                view.removeTextChangedListener(watcher)
            }
        }
    }
    interface CarPartHandlerAdapterListener {
       // fun onCarEnergyTextChange(str: String,position: Int)
        fun onBatteryDelete()
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val stockName: TextView = view.findViewById(R.id.stockName)
        val batteryCode: EditText = view.findViewById(R.id.batteryCode)
        val stockNum: EditText = view.findViewById(R.id.stockNum)
        val moduleCount: EditText = view.findViewById(R.id.moduleCount)
        val weight: EditText = view.findViewById(R.id.weight)
        val capacityAh: EditText = view.findViewById(R.id.capacityAh)
        val voltage: EditText = view.findViewById(R.id.voltage)
        val capacityKWh: EditText = view.findViewById(R.id.capacityKWh)
        val mRemarkET: EditText = view.findViewById(R.id.mRemarkET)
        val mPhotoNGV: NineGridView=view.findViewById(R.id.mPhotoNGV)
        val moduleCountLL: LinearLayoutCompat =view.findViewById(R.id.moduleCountLL)
        val stockNumStart:TextView=view.findViewById(R.id.stockNumStart)
        val moduleCountLine:View=view.findViewById(R.id.moduleCountLine)
        val mRecyclerView:RecyclerView=view.findViewById(R.id.mRecyclerView)
        val mDictionarySelectLL:LinearLayoutCompat=view.findViewById(R.id.mDictionarySelectLL)
        val mBatteryFlag:TextView=view.findViewById(R.id.mBatteryFlag)
        val mDeleteIV:ImageFilterView=view.findViewById(R.id.mDeleteIV)
        val stockPosi:EditText = view.findViewById(R.id.stockPosi)
        val bulkInOrderNo:EditText= view.findViewById(R.id.bulkInOrderNo)
        //val stockPosiStart: TextView = view.findViewById(R.id.stockPosiStart)
    }
}